
package org.vasil.jouikov.services

class BookmakerFeed {

    def static final BOOKMAKER_LINK = 'http://10.0.1.93/Sentinel/ReadData.ashx?Sw=20'

    def updatePrice = { items ->

        def code, action, result
        items.each { sku, item ->
            code = item.status['code']
            action = item.action['type']

            if((code == '200' || code == '201') && action != 'delete') {

                result = savePrice(sku, item.field['sellprice'], item.field['reslprice'])
                if(result != 'Error') {
                    item.status['accware'] = 'ok'
                }
            }
        }
    }

    def savePrice = { accwareid, sellprice, reslprice ->

        try {
            def conn = "${BOOKMAKER_LINK}&SKUBM=${accwareid}&PRC=${sellprice}&DPRC=${reslprice}".toURL().openConnection()
            conn.setRequestMethod('GET')

            if(conn.responseCode == 200 || conn.responseCode == 201) {
                return ['status': 'Ok', 'body': conn.content.text]
            }
            return ['status': 'Err', 'body': "${conn.responseCode} : ${conn.responseMessage}"]
        }
        catch(error) {
            return ['status': 'Err', 'body': error]
        }
    }

}

